home *** CD-ROM | disk | FTP | other *** search
- unit GLPickForm;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- BasicOpenGL, OpenGL12, ComCtrls, ExtCtrls, StdCtrls, MMSystem;
-
- type
- TForm1 = class(TForm)
- TV1: TTreeView;
- Panel2: TPanel;
- ZoomIn: TButton;
- ZoomOut: TButton;
- ViewPos: TComboBox;
- StaticText1: TStaticText;
- procedure FormShow(Sender: TObject);
- procedure FormDestroy(Sender: TObject);
- procedure ZoomInClick(Sender: TObject);
- procedure ZoomOutClick(Sender: TObject);
- procedure TV1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- procedure ViewPosChange(Sender: TObject);
- private
- { Private declarations }
- Procedure UpdateTree;
- public
- { Public declarations }
- OpenGLControl : TBasicOpenGL; //manual instance of the Control
- RootObject : TOpenGLObject; //Object set
- end;
-
- var
- Form1: TForm1;
-
- implementation
-
- {$R *.DFM}
-
- Function GetGLObject(Node:TTreeNode):TOpenGLObject;
- Begin
- //a helper which will extract the TOpenGLObject which is attached to the TreeView Node
- Result:=nil;
- If (Node=nil) or
- (node.data=nil) then exit;
- Result:=TOpenGLObject(Node.data);
- end;
-
- procedure TForm1.FormShow(Sender: TObject);
- Var Translate : TGLPoint;
- Scale : TGLPoint;
- Rotate : TGLPoint;
- TempGL,TempGL1,TempGL2: TOpenGLObject;
- begin
- //create the control
- ViewPos.ItemIndex:=0;
-
- OpenGLControl := TBasicOpenGL.Create(self);
- OpenGLControl.Parent:=self;
- With OpenGLControl do
- Begin
- Align:=alClient; //fill the client
- GLStartUp; //START UP THE GL SESSION
- OnUpdateTreeView:=UpdateTree; //set the call back to update the treeview
- end;
-
- RootObject := TOpenGLObject.create; //create a root object
- RootObject.Text:='Robot';
- OpenGLControl.RootObject:=RootObject; // connect the OpenGL object to the control
-
- Scale.X:=1;Scale.Y:=1;Scale.Z:=1;
- Rotate.X:=0;Rotate.Y:=0;Rotate.Z:=0;
- Translate.X:=0;Translate.X:=0;Translate.Z:=0;
-
- TempGL:= RootObject.addChild;
- TempGL.Text:='Body';
- Translate.X:=0; Translate.Y:=4;Translate.Z:=0;
- Scale.X:=3;Scale.Y:=7;Scale.Z:=3;
- //this X translation will also be applied to all children
- TempGL.Translation:=Translate;
- TempGL.scale:=scale;
- TempGL.Mode:=2;
- //Head
- TempGL1:=TempGL.AddChild;
- TempGL1.Text:='Head';
- TempGL1.Mode:=1;
- Translate.X:=0; Translate.Y:=5.2;Translate.Z:=0;
- Scale.X:=2.5;Scale.Y:=3;Scale.Z:=2.5;
- TempGL1.Translation:=Translate;
- TempGL1.Scale:=Scale;
-
- //Left arm
- TempGL1:=TempGL.AddChild;
- TempGL1.Text:='Left_Arm';
- TempGL1.Mode:=3;
- Translate.X:=2.2; Translate.Y:=0;Translate.Z:=0;
- Scale.X:=0.8;Scale.Y:=0.8;Scale.Z:=6;
- Rotate.X:=90;
- TempGL1.Scale:=Scale;
- TempGL1.Translation:=Translate;
- TempGL1.Rotation:=Rotate;
- //Right arm
- TempGL1:=TempGL.AddChild;
- TempGL1.Text:='Right_Arm';
- TempGL1.Mode:=3;
- Translate.X:=-2.2; Translate.Y:=0;Translate.Z:=0;
- Scale.X:=0.8;Scale.Y:=0.8;Scale.Z:=6;
- Rotate.X:=90;
- TempGL1.Scale:=Scale;
- TempGL1.Translation:=Translate;
- TempGL1.Rotation:=Rotate;
- //Left leg
- TempGL1:=TempGL.AddChild;
- TempGL1.Text:='Left_Leg';
- TempGL1.Mode:=3;
- Translate.X:=1; Translate.Y:=-7.5;Translate.Z:=0;
- Scale.X:=1;Scale.Y:=1;Scale.Z:=8;
- Rotate.X:=90;
- TempGL1.Scale:=Scale;
- TempGL1.Translation:=Translate;
- TempGL1.Rotation:=Rotate;
- //left foot
- TempGL2:=TempGL1.AddChild;
- TempGL2.Text:='Left_Foot';
- TempGL2.Mode:=2;
- Translate.X:=0; Translate.Y:=1;Translate.Z:=4.5;
- Scale.X:=1;Scale.Y:=3;Scale.Z:=1;
- TempGL2.Scale:=Scale;
- TempGL2.Translation:=Translate;
- //Right leg
- TempGL1:=TempGL.AddChild;
- TempGL1.Text:='Right_Leg';
- TempGL1.Mode:=3;
- Translate.X:=-1; Translate.Y:=-7.5;Translate.Z:=0;
- Scale.X:=1;Scale.Y:=1;Scale.Z:=8;
- Rotate.X:=90;
- TempGL1.Scale:=Scale;
- TempGL1.Translation:=Translate;
- TempGL1.Rotation:=Rotate;
- //Right foot
- TempGL2:=TempGL1.AddChild;
- TempGL2.Text:='Right_Foot';
- TempGL2.Mode:=2;
- Translate.X:=0; Translate.Y:=1;Translate.Z:=4.5;
- Scale.X:=1;Scale.Y:=3;Scale.Z:=1;
- TempGL2.Scale:=Scale;
- TempGL2.Translation:=Translate;
-
- UpdateTree; // update the tree view
- end;
-
- Procedure TForm1.UpdateTree;
- Var
- selectID: Integer;
- begin
- //attempt to keep the treeview in sync with the scene graph
- // I hate this control!!
- TV1.Items.BeginUpdate;
- If TV1.Selected<>Nil then
- selectID:=TV1.Selected.AbsoluteIndex
- else
- selectID:=0;
- TV1.Items.Clear;
- RootObject.BuildTreeView(TV1,nil);
- TV1.Selected:=TV1.Items[selectID];
-
- If TV1.Selected<>Nil then
- TV1.Selected.Expand(True);
- TV1.Items.EndUpdate;
- end;
-
- procedure TForm1.FormDestroy(Sender: TObject);
- begin
- RootObject.Free; //tidy up the GlOpbject
- end;
-
- procedure TForm1.ZoomInClick(Sender: TObject);
- begin
- OpenGLControl.ViewAngle:= OpenGLControl.ViewAngle*0.9;
- //reduce the view angle to zoom in
- end;
-
- procedure TForm1.ZoomOutClick(Sender: TObject);
- begin
- OpenGLControl.ViewAngle:= OpenGLControl.ViewAngle/0.9;
- //increase the view angle to zoom out
- end;
-
- procedure TForm1.TV1MouseUp(Sender: TObject; Button: TMouseButton;
- Shift: TShiftState; X, Y: Integer);
- Var GL:TOpenGLObject;
- begin
- //handle selection within the treeview
- RootObject.Selected:=false;
- If TV1.Selected=nil then exit;
- GL:=GetGLObject(TV1.Selected);
- If GL=nil then exit;
- GL.Selected:=True;
- UpdateTree;
- If assigned(OpenGLControl) then
- OpenGLControl.Repaint;
- end;
-
- procedure TForm1.ViewPosChange(Sender: TObject);
- begin
- OpenGLControl.ViewPosition:=ViewPos.ItemIndex;
- OpenGLControl.repaint;
- end;
-
- end.
-